package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableUser;
import cn.ac.jaas.isc.vegetable.common.security.util.SecurityUtils;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcManufacturer;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentMapper;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcManufacturerMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcModel;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcModelMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcModelService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 张高昌
 * @date 2022-05-02 12:52:09
 */
@Service
@AllArgsConstructor
public class ArcModelServiceImpl extends ServiceImpl<ArcModelMapper, ArcModel> implements ArcModelService {

    private final ArcManufacturerMapper arcManufacturerMapper;

    private final ArcEquipmentMapper arcEquipmentMapper;

    @Override
    public Page getModelPage(Page page, ArcModel arcModel) {
        Page page1 = baseMapper.selectPage(page, Wrappers.query(arcModel));
        return page1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveModel(ArcModel arcModel) {
        LambdaQueryWrapper<ArcModel> nameWrapper = Wrappers.lambdaQuery();
        nameWrapper.eq(ArcModel::getModelName, arcModel.getModelName())
                .eq(ArcModel::getManufacturerCode, arcModel.getManufacturerCode());
        ArcModel nameModel = baseMapper.selectOne(nameWrapper);
        if (nameModel != null) {
            return R.failed("型号名称已存在");
        }
        LambdaQueryWrapper<ArcModel> codeWrapper = Wrappers.lambdaQuery();
        codeWrapper.eq(ArcModel::getModelCode, arcModel.getModelCode())
                .eq(ArcModel::getManufacturerCode, arcModel.getManufacturerCode());
        ArcModel codeModel = baseMapper.selectOne(codeWrapper);
        if (codeModel != null) {
            return R.failed("型号编码已存在");
        }
        ArcManufacturer arcManufacturer = arcManufacturerMapper.selectOne(Wrappers.<ArcManufacturer>query().lambda().eq(ArcManufacturer::getManufacturerCode, arcModel.getManufacturerCode()));
        if (arcManufacturer == null) {
            return R.failed("厂商不存在！");
        }

        VegetableUser user = SecurityUtils.getUser();
        arcModel.setModelCode(arcManufacturer.getManufacturerCode() + "-" + arcModel.getModelCode());
        arcModel.setCreateTime(LocalDateTime.now());
        arcModel.setCreateUser(user.getId());
        baseMapper.insert(arcModel);

        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateModelById(ArcModel arcModel) {
        LambdaQueryWrapper<ArcModel> nameWrapper = Wrappers.lambdaQuery();
        nameWrapper.eq(ArcModel::getModelName, arcModel.getModelName())
                .eq(ArcModel::getManufacturerCode, arcModel.getManufacturerCode())
                .ne(ArcModel::getModelId, arcModel.getModelId());
        ArcModel nameModel = baseMapper.selectOne(nameWrapper);
        if (nameModel != null) {
            return R.failed("型号名称已存在");
        }
        LambdaQueryWrapper<ArcModel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcModel::getModelCode, arcModel.getModelCode())
                .eq(ArcModel::getManufacturerCode, arcModel.getManufacturerCode())
                .ne(ArcModel::getModelId, arcModel.getModelId());
        ArcModel codeModel = baseMapper.selectOne(wrapper);
        if (codeModel != null) {
            return R.failed("型号编码已存在");
        }

        VegetableUser user = SecurityUtils.getUser();
        arcModel.setUpdateTime(LocalDateTime.now());
        arcModel.setUpdateUser(user.getId());
        baseMapper.updateModelId(arcModel);
        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R removeModelById(Integer modelId) {
        ArcModel arcModel = baseMapper.selectById(modelId);
        List<ArcEquipment> arcEquipments = arcEquipmentMapper.selectList(Wrappers.<ArcEquipment>query().lambda().eq(ArcEquipment::getModelCode, arcModel.getModelCode()));
        if (arcEquipments.size() > 0) {
            return R.failed("该型号已在使用中，无法删除");
        }
        baseMapper.deleteById(modelId);
        return R.ok(true);
    }
}
